package com.aididi.concurrent;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Auther: zx.xu
 * @Description:    自定义拒绝策略,保证每个任务都会被处理
 */
public class CustomRejectHandler implements RejectedExecutionHandler{
    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        try {
            //尝试阻塞式加入任务队列
            executor.getQueue().put(r);
        } catch (Exception e) {
            //保持线程的中断状态
            Thread.currentThread().interrupt();
        }
    }
}
